package com.activity.fragment;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.apple.ddhr_new.R;
import com.example.lzbase.BaseFragment;
import com.example.lzbase.BaseViewModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import utils.ClassUtil;

public abstract class BaseListViewFragment<VM extends BaseViewModel> extends BaseFragment {

    @BindView(R.id.recycle_view)
    protected RecyclerView recycleView;

    public SmartRefreshLayout getRefreshLayout() {
        return refreshLayout;
    }

    @BindView(R.id.refreshLayout)
    protected SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_data)
    protected TextView tvData;
    @BindView(R.id.l_empty)
    protected LinearLayout lEmpty;
    @BindView(R.id.iv_nouse)
    protected ImageView iv_nouse;

    private CompositeDisposable mCompositeDisposable;
    protected VM viewModel;

    public void addSubscription(Disposable s) {
        if (this.mCompositeDisposable == null) {
            this.mCompositeDisposable = new CompositeDisposable();
        }
        this.mCompositeDisposable.add(s);
    }

    @Override
    public void createView() {


        initViewModel();

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                headerFreshing();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                footerFreshing();
            }
        });

        recycleView.setLayoutManager(new LinearLayoutManager(mContext));
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_refresh;
    }



    /**
     * 初始化ViewModel
     */
    private void initViewModel() {
        Class<VM> viewModelClass = ClassUtil.getViewModel(this);
        if (viewModelClass != null) {
            ViewModelProvider viewModelProvider = new ViewModelProvider(this.getViewModelStore(), new ViewModelProvider.AndroidViewModelFactory(((Activity) mContext).getApplication()));

            this.viewModel = viewModelProvider.get(viewModelClass);
        }


        if (this.viewModel != null) {
            this.viewModel.error.observe(this, error -> {
                onError(error);
            });
            this.viewModel.progress.observe(this, show -> {

                showLoading(show);
            });
        }


    }

    @Override
    public void onDestroy() {
        super.onDestroy();


        if (this.mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
            // clear 和 dispose的区别是：  disposed = true;
            this.mCompositeDisposable.clear();
        }
    }

    public void onError(Throwable error) {
        if (null != error.getMessage()) {
            Toast.makeText(mContext, error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    protected void headerFreshing() {
        headerFreshEnd();

    }

    protected void footerFreshing() {
        footerFreshEnd();
    }

    protected void headerFresh() {
        refreshLayout.autoRefresh();

    }

    protected void footerFresh() {
        refreshLayout.autoLoadMore();
    }

    protected void headerFreshEnd() {
        refreshLayout.finishRefresh();

    }

    protected void footerFreshEnd() {
        refreshLayout.finishLoadMore();
    }

    protected void showEmpty(boolean isEmpty) {
        if(isEmpty){

//            refreshLayout.setVisibility(View.GONE);
            lEmpty.setVisibility(View.VISIBLE);
        }else
        {
//            refreshLayout.setVisibility(View.VISIBLE);
            lEmpty.setVisibility(View.GONE);
        }
    }
}
